<?php
/**
 * Created by JetBrains PhpStorm.
 * User: DoTuan
 * Date: 10/22/13
 * Time: 12:58 PM
 * To change this template use File | Settings | File Templates.
 */
class Admin extends CI_Controller
{
    public $_show;

    public function __construct()
    {
        parent::__construct();
        $this->load->helper("url");
        $this->load->library('session');
        // load model banner
        $this->load->model('Model_Banner');
        // load model banning
        $this->load->model('Model_Banning');
        $this->load->model("model_admin");
    }

    public function index()
    {
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/index";
            $this->_show['date'] = "";
            $this->load->view('admin/layout1/body', $this->_show);
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }

    public function login()
    {
        $this->_show['title'] = "Admin Management Page";

        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $url = base_url()."admin/";
            header("Location:$url");
        }else{
            if(!empty($_POST)){
                $username = "";
                $password = "";
                if(isset($_POST['username'])){
                    $username = $_POST['username'];
                }
                if(isset($_POST['password'])){
                    $password = $_POST['password'];
                }
                $login = $this->model_admin->login($username, $password);
                if($login>0){
                    $this->session->set_userdata('userInfo', $login);
                    $url = base_url()."admin/";
                    header("Location:$url");
                }else{
                    $this->_show['error'] = "Kiểm tra đăng nhập.";
                }
            }else{
                $this->_show['error'] = "";
            }
        }
        $this->load->view('admin/login', $this->_show);
    }
    public function logout(){
        $this->session->unset_userdata('userInfo');
        $url = base_url()."admin/login";
        header("Location:$url");
    }
    public function search()
    {
        $this->_show['title'] = "Admin Management Page";
        $this->_show['view'] = "admin/search_user";
        $this->_show['date'] = "";
        $this->load->view('admin/layout/content', $this->_show);
    }

    public function search_detail()
    {
        $this->_show['title'] = "Admin Management Page";
        $this->_show['view'] = "admin/search_detail";
        $this->_show['date'] = "";
        $this->load->view('admin/layout/content', $this->_show);
    }

    public function user_info()
    {
        $this->_show['title'] = "Admin Management Page";
        $this->_show['view'] = "admin/user_info";
        $this->_show['date'] = "";
        $this->_show['load_email'] = ""; // value Email
        $this->_show['load_phone'] = ""; // value số điện thoại
        $this->_show['load_name'] = ""; // value tên user
        $this->_show['name_button'] = "Tạo"; // value tên button
        /*
         * (Không xóa nha, Có gì liên hệ Tiến)
         * Trước đó bấm click vào email ở trang banning
         * Load du lieu đổ ra page theo userid
         */
        if (isset($_GET['idbanning'])) {
            $list_user = $this->Model_Banning->getListUser();
            foreach ($list_user as $khoa => $value) {
                if ($value['UserId'] == $_GET['idbanning']) {
                    $this->_show['load_email'] = $value['Email'];
                    $this->_show['load_phone'] = $value['PhoneNumer'];
                    $this->_show['load_name'] = $value['UserName'];
                    $this->_show['name_button'] = "Cập Nhật";
                    break;
                }
            }
        }


        $this->load->view('admin/layout/content', $this->_show);
    }

    // Cấm thành viên

    public function banner()
    {
        $useInfor = $this->session->userdata('userInfo');
       if($useInfor){
        $this->_show['title'] = "Admin Management Page";
        $this->_show['view'] = "admin/banner";
        $this->_show['date'] = "";
        // Show list email
        $this->_show['listUser'] = $this->Model_Banner->getListUserNameBanner();
        // Show list group
        $this->_show['listGroup'] = $this->Model_Banner->getListGroup();

        $this->_show['load_user'] = ""; // value Email
        $this->_show['load_whybanner'] = ""; // value ly do
        $this->_show['name_button'] = "Thực Hiện"; // value ten Button


        /* Trước đó có pải bấm chỉnh sửa ở trang banning
         * Nếu đúng, thì load email và lý do bị banner
         */
        if (isset($_GET['idban'])) {
            $list_ban = $this->Model_Banner->getBanner();
            foreach ($list_ban as $khoa => $value) {
                if ($value['id'] == $_GET['idban']) {
                    $this->_show['load_user'] = $value['UserName'];
                    $this->_show['load_whybanner'] = $value['note'];
                    $this->_show['name_button'] = "Cập Nhật";
                    break;
                }
            }
        }

        // Bam nut thuc hien
        if (isset($_POST['btn-register-user-info'])) {
            if (isset($_POST['user_banner']) &&
                $_POST['user_banner'] != "" &&
                isset($_POST['time_banner']) &&
                isset($_POST['group_banner']) &&
                $_POST['why_banner'] != "" &&
                isset($_POST['why_banner'])
            ) {
                // check User Name co ton tai
                $check_user = $this->Model_Banner->checkUserNamelBanner($_POST['user_banner']);

                if ($check_user == "1") {

                    $status = "true";
                    if ($_POST['group_banner'] == 3) {
                        $status = "true";
                    } else {
                        $status = "false";
                    }

                    // khai bao cac bien truyen vao
                    $userid = $this->Model_Banner->getUseridbyUserName($_POST['user_banner']);
                    $Date = date("Y-m-d");
                    $timeban = date('d/m/Y', strtotime($Date . ' + ' . $_POST['time_banner'] . ' days'));
                    $note = $_POST['why_banner'];
                    if (isset($_GET['idban'])) {

                        // Cap Nhat banner
                        $URL = "http://egov047.juddy.vn/api/ban/update?id=" . $_GET['idban'] . "&userid=" . $userid . "&timeban=" . $timeban . "&note=" . $note . "&status=" . $status;
                        $ch = curl_init();
                        $url1 = str_replace(" ", '%20', $URL);
                        curl_setopt($ch, CURLOPT_URL, $url1);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        curl_exec($ch);
                        $this->_show['alert'] = "<script>  alert('Cập nhật banner thành công ')</script>";
                        $this->_show['load_user'] = ""; // value Email
                        $this->_show['load_whybanner'] = ""; // value ly do
                        $this->_show['name_button'] = "Thực Hiện"; // value ten Button

                    } else if (!isset($_GET['idban'])) {
                        // Insert  len web service
                        $URL = "http://egov047.juddy.vn/api/ban/insert?userid=" . $userid . "&timeban=" . $timeban . "&note=" . $note . "&status=" . $status;
                        $ch = curl_init();
                        $url1 = str_replace(" ", '%20', $URL);
                        curl_setopt($ch, CURLOPT_URL, $url1);
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        curl_exec($ch);
                        $this->_show['alert'] = "<script>  alert('Thêm banner thành công ')</script>";
                    }

                } else {
                    $this->_show['alert'] = "<script>  alert('User Name không tồn tại???!!!  Mời bạn nhập lại. ')</script>";
                }
            } else {
                $this->_show['alert'] = "<script>  alert('Chưa nhập thông tin đầy đủ???!!! Mời bạn nhập thêm. ')</script>";
            }
        }

        $this->load->view('admin/layout1/body', $this->_show);
         }else{
           $url = base_url()."index.php/admin/login";
           header("Location:$url");
        }
    }

    // Thành viên bị cấm
    public function banning()
    {
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
        $this->_show['title'] = "Admin Management Page";
        $this->_show['view'] = "admin/banning";
        $this->_show['date'] = "";

        // Lay du lieu tu web service
        $this->_show['data'] = $this->Model_Banning->listBanning();

        $this->load->view('admin/layout1/body', $this->_show);
    }else{
        $url = base_url()."index.php/admin/login";
        header("Location:$url");
        }
    }


    public function role()
    {
        $this->_show['title'] = "Admin Management Page";
        $this->_show['view'] = "admin/role";
        $this->_show['date'] = "";
        $this->load->view('admin/layout/content', $this->_show);
    }


    public function threads($action = Null)
    {        
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $data['content']    = $action;

            
            $data['backend']       = base_url().'admin/threads/';
            
            $data['title']         = "Admin Management Page";
            $data['view']          = "admin/threads";
            $param                 = $this->uri->segment(3);

            $config['base_url']    = base_url().'admin/threads';   
            $news = $this->model_admin->getAllPlace();   

            $loca = $this->model_admin->getLocation(); 
            $user = $this->model_admin->getUser();  

            $sort_url = array();
            $sort_by_date = "";
            $sort_by_loca = "";
            $sort_by_user = "";
            $sort_by_status = "";
            $sort_by_pri = "";

            if (isset($param)) {
                if ($param == "Date_asc") {
                   $sort_url = $this->model_admin->array_sort($news, "DateCreated", SORT_ASC);
                   $sort_by_date = "Date_desc";
                }
                elseif ($param == "Date_desc") {
                   $sort_url = $this->model_admin->array_sort($news, "DateCreated", SORT_DESC);
                   $sort_by = "Date_asc";
                }
                elseif ($param == "Loca_asc") {
                    $sort_url = $this->model_admin->array_sort($news, "LocationId", SORT_ASC);
                   $sort_by_loca = "Loca_desc";
                }
                elseif ($param == "Loca_desc") {
                    $sort_url = $this->model_admin->array_sort($news, "LocationId", SORT_DESC);
                   $sort_by = "Loca_asc";
                }
                elseif ($param == "User_asc") {
                    $sort_url = $this->model_admin->array_sort($news, "UserId", SORT_ASC);
                   $sort_by_user = "User_desc";
                }
                elseif ($param == "User_desc") {
                    $sort_url = $this->model_admin->array_sort($news, "UserId", SORT_DESC);
                   $sort_by = "User_asc";
                }
                elseif ($param == "Status_asc") {
                    $sort_url = $this->model_admin->array_sort($news, "status", SORT_ASC);
                   $sort_by_status = "Status_desc";
                }
                elseif ($param == "Status_desc") {
                    $sort_url = $this->model_admin->array_sort($news, "status", SORT_DESC);
                   $sort_by = "Status_asc";
                }
                elseif ($param == "Priority_asc") {
                    $sort_url = $this->model_admin->array_sort($news, "Priority", SORT_ASC);
                   $sort_by_pri = "Priority_desc";
                }
                elseif ($param == "Priority_desc") {
                    $sort_url = $this->model_admin->array_sort($news, "Priority", SORT_DESC);
                    $sort_by = "Priority_asc";
                }
                else {
                    $sort_url = $news;
                }
            }

            $data['sort_by_date'] = $sort_by_date; 
            $data['sort_by_loca'] = $sort_by_loca; 
            $data['sort_by_user'] = $sort_by_user; 
            $data['sort_by_status'] = $sort_by_status; 
            $data['sort_by_pri'] = $sort_by_pri; 
            $data['news'] = $sort_url; 
            $data['loca'] = $loca; 
            $data['user'] = $user;  

            switch ($data['content']){  
                case 'enable_threads':
                    $param           = $this->uri->segment(4);

                    $t_ = $this->model_admin->getPlaceById($param);
                    $URL = "http://egov047.juddy.vn/api/place/Update?Id=" . $param . "&Title=" . $t_["Title"] . "&Summary=" . $t_["Summary"] .  "&Description=" . urlencode($t_["Description"]) . "&Latitude=" . $t_["Latitude"] . "&Longitude=" . $t_["Longitude"] . "&Address=" . $t_["Address"] . "&LocationId=" . $t_["LocationId"] . "&Priority="  . $t_["Priority"] . "&UserId="  . $t_["UserId"] . "&status=true";
                    
                    $this->model_admin->execURL($URL);
                    redirect(base_url("admin/threads"));
                    break;
                case 'disable_threads':
                    $param           = $this->uri->segment(4);
                    $t_ = $this->model_admin->getPlaceById($param);
                    $URL = "http://egov047.juddy.vn/api/place/Update?Id=" . $param . "&Title=" . $t_["Title"] . "&Summary=" . $t_["Summary"] .  "&Description=" . urlencode($t_["Description"]) . "&Latitude=" . $t_["Latitude"] . "&Longitude=" . $t_["Longitude"] . "&Address=" . $t_["Address"] . "&LocationId=" . $t_["LocationId"] . "&Priority="  . $t_["Priority"] . "&UserId="  . $t_["UserId"] . "&status=false";
                    echo $URL;

                    $this->model_admin->execURL($URL);
                    redirect(base_url("admin/threads"));

                    break;
                case 'edit_threads' : 
                    $param           = $this->uri->segment(4);
                    $data['view']    = "admin/".$data['content'];

                    $threads_edit = $this->model_admin->getPlaceById($param);
                    $loca = $this->model_admin->getLocation();
                    
                    $data['threads_edit'] = $threads_edit;
                    $data['loca'] = $loca;
                    
                    break;
                case 'delete_threads':
                    $param           = $this->uri->segment(4);

                    $t_ = $this->model_admin->getPlaceById($param);
                    $URL = "http://egov047.juddy.vn/api/place/Delete?Id=" . $param;

                    $ch = curl_init();

                    $url1 = str_replace(" ", '%20', $URL);
                    curl_setopt($ch, CURLOPT_URL,$url1);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                    $str = curl_exec($ch);
                    curl_close($ch);

                    echo $url1;
                    var_dump($str);

                    redirect(base_url("admin/threads"));
                    break;
                case 'save_edit':
                    
                    $t_id = $this->input->post("t_id");
                    $t_name = $this->input->post("t_name");
                    $t_sum = $this->input->post("t_sum");
                    $t_lat = $this->input->post("t_lat");
                    $t_long = $this->input->post("t_long");
                    $t_pri = $this->input->post("t_pri");
                    $t_add = $this->input->post("t_add");
                    $t_loca = $this->input->post("t_loca");
                    $t_dec = $this->input->post("t_dec");
                    $t_uid = $this->input->post("t_uid");
                    $t_sta = $this->input->post("t_sta");

                   $URL = "http://egov047.juddy.vn/api/place/Update?Id=" . $t_id . "&Title=" . $t_name . "&Summary=" . $t_sum .  "&Description=" . urlencode($t_dec) . "&Latitude=" . $t_lat . "&Longitude=" . $t_long . "&Address=" . $t_add . "&LocationId=" . $t_loca . "&Priority="  . $t_pri . "&UserId="  . $t_uid . "&status=".$t_sta;

                    $this->model_admin->execURL($URL);
                    redirect(base_url("admin/threads"));

                    break;
            }

            $this->load->view('admin/layout1/body', $data);  
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }

    public function comment($action = Null)
    {     
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $data['content']    = $action;

            
            $data['backend']       = base_url().'admin/comment/';
            
            $data['title']         = "Admin Management Page";
            $data['view']          = "admin/comment";
            $param                 = $this->uri->segment(3);
            # code...
            
            $config['base_url']    = base_url().'admin/comment';   
            $coms = $this->model_admin->getAllComment();   
            $news = $this->model_admin->getAllPlace();   


            $loca = $this->model_admin->getLocation(); 
            $user = $this->model_admin->getUser();      

            $sort_url = array();
            $sort_by_status = "";

            if (isset($param)) {
                if ($param == "Status_asc") {
                    $sort_url = $this->model_admin->array_sort($coms, "status", SORT_ASC);
                   $sort_by_status = "Status_desc";
                }
                elseif ($param == "Status_desc") {
                    $sort_url = $this->model_admin->array_sort($coms, "status", SORT_DESC);
                   $sort_by_status = "Status_asc";
                }
                else {
                    $sort_url = $coms;
                }
            }

            $data['sort_by_status'] = $sort_by_status; 

            $data['coms'] = $sort_url; 
            $data['news'] = $news; 
            $data['user'] = $user; 

            switch ($data['content']){  

                case 'enable_comment':

                    $param           = $this->uri->segment(4);
                    $t_ = $this->model_admin->getCommentById($param);
                    $pl = "";
                    $ct = "";
                    $uid = "";

                    foreach ($t_ as $key => $value) {
                        $pl = $value["PlaceId"];
                        $ct = $value["Content"];
                        $uid = $value["UserId"];
                    }

                    $URL = "http://egov047.juddy.vn/api/Comment/Update?Id=" . $param . "&PlaceId=" . $pl . "&Content=" . $ct . "&UserId="  . $uid . "&status=true";
                   
                    $ch = curl_init();
                    $url1 = str_replace(" ", '%20', $URL);

                    curl_setopt($ch, CURLOPT_URL,$url1);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                    $str = curl_exec($ch);
                    curl_close($ch);
                    var_dump($str);

                    redirect(base_url("admin/comment"));
                    break;

                case 'disable_comment':
                    $param           = $this->uri->segment(4);
                    $t_ = $this->model_admin->getCommentById($param);

                    $pl = "";
                    $ct = "";
                    $uid = "";

                    foreach ($t_ as $key => $value) {
                        $pl = $value["PlaceId"];
                        $ct = $value["Content"];
                        $uid = $value["UserId"];
                    }                

                    $URL = "http://egov047.juddy.vn/api/Comment/Update?Id=" . $param . "&PlaceId=" . $pl . "&Content=" . $ct . "&UserId="  . $uid . "&status=false";
                    
                    $ch = curl_init();
                    $url1 = str_replace(" ", '%20', $URL);
                    curl_setopt($ch, CURLOPT_URL,$url1);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                    $str = curl_exec($ch);
                    curl_close($ch);
                    var_dump($str);

                    redirect(base_url("admin/comment"));
                    break;
                case 'delete_comment':
                    $param           = $this->uri->segment(4);

                    $t_ = $this->model_admin->getPlaceById($param);
                    $URL = "http://egov047.juddy.vn/api/comment/Delete?Id=" . $param;
                    $ch = curl_init();

                    $url1 = str_replace(" ", '%20', $URL);
                    curl_setopt($ch, CURLOPT_URL,$url1);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                    $str = curl_exec($ch);
                    curl_close($ch);
                    var_dump($str);

                    redirect(base_url("admin/comment"));
                    break;
            }

            $this->load->view('admin/layout1/body', $data);  
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }

    //Layout 1
    /**
     * Author DoTuan
     * user management
     */
    public function users(){
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/users";

            $data = $this->model_admin->getUser();
            $group = $this->model_admin->getGroup();
            //pagination
            $currentPage = 1;
            $per_page = 8;
            $numRows = sizeof($data);
            $numPage = ceil($numRows/$per_page);
            $page = ($currentPage - 1)*$per_page;
            $arr = null;
            $j = 0;
            for($i = $page; $i<$page+$per_page; $i++){
                $arr[$j] = $data[$i];
                $j++;
            }

            $this->_show['data'] = $arr;
            $this->_show['group'] = $group;
            $this->_show['numberPage'] = $numPage;
            $this->load->view('admin/layout1/body', $this->_show);
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }
    public function UserPagination(){
        if(isset($_POST['page'])){
            $currentSelect = $_POST['page'];
            $per_page = 8;
            if(isset($_POST['per_page'])){
                $per_page = $_POST['per_page'];
            }
            $data = $this->model_admin->getUser();
            $group = $this->model_admin->getGroup();
            $page = ($currentSelect-1)*$per_page;
            $arr = null;
            $j = 0;
            for($i = $page; $i<$page+$per_page; $i++){
                $arr[$j] = $data[$i];
                $j++;
            }

            echo json_encode(array('status'=>'ok', 'pages'=>$arr, 'group'=>$group));
        }
    }
    public function checkUsername(){
        $username = "";
        if(isset($_POST['username'])){
            $username = $_POST['username'];
        }
        $user  = $this->model_admin->getUserByUserName($username);

        if(sizeof($user)>0){
            echo json_encode(array("status"=>"error"));
        }else{
            echo json_encode(array("status"=>"ok"));
        }
    }
    /**
     * Author DoTuan
     * Create new user
     */
    public function create()
    {
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/create";
            $group  = $this->model_admin->getGroup();
            if(isset($_POST['register_submit'])){
                $username = "";
                $email = "";
                $password = "";
                $phone = "";
                $group = 0;
                //get data
                if(isset($_POST['username'])){
                    $username = $_POST['username'];
                }
                if(isset($_POST['email'])){
                    $email = $_POST['email'];
                }
                if(isset($_POST['password'])){
                    $password = $_POST['password'];
                }
                if(isset($_POST['phone'])){
                    $phone = $_POST['phone'];
                }
                if(isset($_POST['user_group'])){
                    $group = $_POST['user_group'];
                }
                $register = $this->model_admin->register($username, $password, $phone, $email, "","", $group);
                if($register){
                    $id = $this->model_admin->getUserByName($username);
                    $url = base_url()."admin/profile/".$id['UserId'];
                    header("Location:$url");
                }
            }


            $this->_show['group'] = $group;
            $this->_show['date'] = "";
            $this->load->view('admin/layout1/body', $this->_show);
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }
    /**
     * Author DoTuan
     * profile management
     */
    public function profile($uid=0)
    {
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/profile";
            $group  = $this->model_admin->getGroup();

            $userId = 0;
            if(isset($uid) && $uid >0){
                $data = $this->model_admin->getUserById($uid);
                $userId = $uid;
            }else{
                $data = $this->model_admin->getUserById($useInfor);
                $userId = $useInfor;
            }

            if(isset($_POST['update_submit'])){
                $groupId = 0;
                $email ="";
                $name = "";
                $phone = "";

                if(isset($_POST['email'])){
                    $email = $_POST['email'];
                }

                $username = $data['UserName'];
                $avatar = $data['Avatar'];
                $bio = $data['Bio'];

                if(isset($_POST['group'])){
                    $groupId = $_POST['group'];
                }

                if(isset($_POST['name'])){
                    $name = $_POST['name'];
                }
                if(isset($_POST['phone'])){
                    $phone = $_POST['phone'];
                }
                $update = $this->model_admin->updateUser($userId, $username, $phone, $email, $avatar, $bio, $groupId);
            }

            if(isset($uid) && $uid >0){
                $data = $this->model_admin->getUserById($uid);
            }else{
                $data = $this->model_admin->getUserById($useInfor);
            }

            $this->_show['group'] = $group;
            $this->_show['username'] = $data['UserName'];
            $this->_show['email'] = $data['Email'];
            $this->_show['phone'] = $data['PhoneNumer'];
            $this->_show['gid'] = $data['GroupId'];

            $this->_show['data'] = $data;

            $this->load->view('admin/layout1/body', $this->_show);
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }

    public function group()
    {
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/group";
            $group  = $this->model_admin->getGroup();



            $this->_show['group'] = $group;
            $this->_show['date'] = "";
            $this->load->view('admin/layout1/body', $this->_show);
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }
    public function deleteGroup(){

    }
    public function new_group()
    {
        $this->_show['title'] = "Admin Management Page";
        $this->_show['view'] = "admin/new_group";
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            if(isset($_POST['submit_new_group'])){
                $name = "";
                $description = "";
                if(isset($_POST['group_name'])){
                    $name = $_POST['group_name'];
                }
                if(isset($_POST['group_description'])){
                    $description = $_POST['group_description'];
                }
                $addGroup = $this->model_admin->addGroup($name, $description);
                $gid = 0;
                if($addGroup == 1){
                    $groupInfor = $this->model_admin->getGroupByName($name);
                    foreach($groupInfor as $value){
                        $gid = $value['GroupId'];
                    }
                    //management role
                    /** Role management*/
                    $loginAdmin = $_POST['login_admin'];
                    if($loginAdmin != ""){
                        $this->model_admin->addRole($gid, $loginAdmin, "Management");
                    }
                    $banUser = $_POST['ban_user'];
                    if($banUser != ""){
                        $this->model_admin->addRole($gid, $banUser, "Management");
                    }
                    $groupManagement = $_POST['group_management'];
                    if($groupManagement != ""){
                        $this->model_admin->addRole($gid, $groupManagement, "Management");
                    }
                    /** Role threads*/
                    $createThreads = $_POST['create_threads'];
                    if($createThreads != ""){
                        $this->model_admin->addRole($gid, $createThreads, "Threads");
                    }
                    $viewThreads = $_POST['view_threads'];
                    if($viewThreads != ""){
                        $this->model_admin->addRole($gid, $viewThreads, "Threads");
                    }
                    $editThreads = $_POST['edit_threads'];
                    if($editThreads != ""){
                        $this->model_admin->addRole($gid, $editThreads, "Threads");
                    }
                    $deleteThreads = $_POST['delete_threads'];
                    if($deleteThreads != ""){
                        $this->model_admin->addRole($gid, $deleteThreads, "Threads");
                    }
                    /** Role Comment*/
                    $createComment = $_POST['create_comment'];
                    if($createComment != ""){
                        $this->model_admin->addRole($gid, $createComment, "Comment");
                    }
                    $commentNotCheck = $_POST['comment_not_check'];
                    if($commentNotCheck != ""){
                        $this->model_admin->addRole($gid, $commentNotCheck, "Comment");
                    }
                    $commentColor = $_POST['comment_color'];
                    if($commentColor != ""){
                        $this->model_admin->addRole($gid, $commentColor, "Comment");
                    }
                    /** Role sign*/
                    $createSign = $_POST['create_sign'];
                    if($createSign != ""){
                        $this->model_admin->addRole($gid, $createSign, "Sign");
                    }
                    $imageSign = $_POST['image_sign'];
                    if($imageSign != ""){
                        $this->model_admin->addRole($gid, $imageSign, "Sign");
                    }
                    $signSize = $_POST['sign_size'];
                    if($signSize != ""){
                        $this->model_admin->addRole($gid, $signSize, "Sign");
                    }
                    /** Role avatar*/
                    $createAvatar = $_POST['create_avatar'];
                    if($createAvatar != ""){
                        $this->model_admin->addRole($gid, $createAvatar, "Avatar");
                    }
                    $editAvatar = $_POST['edit_avatar'];
                    if($editAvatar != ""){
                        $this->model_admin->addRole($gid, $editAvatar, "Avatar");
                    }
                    $sizeAvatar = $_POST['size_avatar'];
                    if($sizeAvatar != ""){
                        $this->model_admin->addRole($gid, $sizeAvatar, "Avatar");
                    }
                    $url = base_url()."admin/group";
                    header("Location:$url");
                }

            }


            $this->_show['date'] = "";
            $this->load->view('admin/layout1/body', $this->_show);
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }
    public function management($gid=0){
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/management";
            $name = "";
            $description = "";

            if($gid > 0){
                $data = $this->model_admin->getGroupById($gid);
                foreach($data as $value){
                    $name = $value['GroupName'];
                    $description = $value['Description'];
                }
                if(isset($_POST['update_group'])){

                    $group_name = "";
                    $group_description = "";
                    if(isset($_POST['group_name'])){
                        $group_name = $_POST['group_name'];
                    }
                    if(isset($_POST['group_description'])){
                        $group_description = $_POST['group_description'];
                    }
                    $update = $this->model_admin->updateGroup($gid, $group_name, $group_description);
                    $role = $this->model_admin->getRoleByGroupId($gid);

                    //Management
                    $roleLoginAdmin = "LoginAdmin";
                    if(isset($_POST['login_admin']) && $_POST['login_admin'] == $roleLoginAdmin){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleLoginAdmin){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleLoginAdmin, "Management");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleLoginAdmin){
                                $this->model_admin->deleteRole($gid, $roleLoginAdmin);
                            }
                        }
                    }

                    $roleBanUser = "BanUser";
                    if(isset($_POST['ban_user']) && $_POST['ban_user'] == $roleBanUser){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleBanUser){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleBanUser, "Management");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleBanUser){
                                $this->model_admin->deleteRole($gid, $roleBanUser);
                            }
                        }
                    }

                    $roleGroupManagement = "GroupManagement";
                    if(isset($_POST['group_management']) && $_POST['group_management'] == $roleGroupManagement){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleGroupManagement){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleGroupManagement, "Management");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleGroupManagement){
                                $this->model_admin->deleteRole($gid, $roleGroupManagement);
                            }
                        }
                    }
                    //Threats
                    $roleCreateThreads = "CreateThreads";
                    if(isset($_POST['create_threads']) && $_POST['create_threads'] == $roleCreateThreads){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCreateThreads){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleCreateThreads, "Threads");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCreateThreads){
                                $this->model_admin->deleteRole($gid, $roleCreateThreads);
                            }
                        }
                    }

                    $roleViewThreads = "ViewThreads";
                    if(isset($_POST['view_threads']) && $_POST['view_threads'] == $roleViewThreads){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleViewThreads){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleViewThreads, "Threads");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleViewThreads){
                                $this->model_admin->deleteRole($gid, $roleViewThreads);
                            }
                        }
                    }

                    $roleEditThreats = "EditThreats";
                    if(isset($_POST['edit_threads']) && $_POST['edit_threads'] == $roleEditThreats){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleEditThreats){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleEditThreats, "Threads");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleEditThreats){
                                $this->model_admin->deleteRole($gid, $roleEditThreats);
                            }
                        }
                    }

                    $roleDeleteThreads = "DeleteThreads";
                    if(isset($_POST['delete_threads']) && $_POST['delete_threads'] == $roleDeleteThreads){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleDeleteThreads){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleDeleteThreads, "Threads");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleDeleteThreads){
                                $this->model_admin->deleteRole($gid, $roleDeleteThreads);
                            }
                        }
                    }

                    //Comment
                    $roleCreateComment = "CreateComment";
                    if(isset($_POST['create_comment']) && $_POST['create_comment'] == $roleCreateComment){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCreateComment){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleCreateComment, "Comment");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCreateComment){
                                $this->model_admin->deleteRole($gid, $roleCreateComment);
                            }
                        }
                    }

                    $roleCommentNotCheck = "CommentNotCheck";
                    if(isset($_POST['comment_not_check']) && $_POST['comment_not_check'] == $roleCommentNotCheck){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCommentNotCheck){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleCommentNotCheck, "Comment");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCommentNotCheck){
                                $this->model_admin->deleteRole($gid, $roleCommentNotCheck);
                            }
                        }
                    }

                    $roleCommentColor = "CommentColor";
                    if(isset($_POST['comment_color']) && $_POST['comment_color'] == $roleCommentColor){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCommentColor){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleCommentColor, "Comment");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCommentColor){
                                $this->model_admin->deleteRole($gid, $roleCommentColor);
                            }
                        }
                    }
                    //Sign
                    $roleCreateSign = "CreateSign";
                    if(isset($_POST['create_sign']) && $_POST['create_sign'] == $roleCreateSign){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCreateSign){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleCreateSign, "Sign");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCreateSign){
                                $this->model_admin->deleteRole($gid, $roleCreateSign);
                            }
                        }
                    }
                    $roleImageSign = "ImageSign";
                    if(isset($_POST['image_sign']) && $_POST['image_sign'] == $roleImageSign){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleImageSign){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleImageSign, "Sign");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleImageSign){
                                $this->model_admin->deleteRole($gid, $roleImageSign);
                            }
                        }
                    }
                    $roleSignSize = "SignSize";
                    if(isset($_POST['sign_size']) && $_POST['sign_size'] == $roleSignSize){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleSignSize){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleSignSize, "Sign");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleSignSize){
                                $this->model_admin->deleteRole($gid, $roleSignSize);
                            }
                        }
                    }
                    //Avatar
                    $roleCreateAvatar = "CreateAvatar";
                    if(isset($_POST['create_avatar']) && $_POST['create_avatar'] == $roleCreateAvatar){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCreateAvatar){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleCreateAvatar, "Avatar");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleCreateAvatar){
                                $this->model_admin->deleteRole($gid, $roleCreateAvatar);
                            }
                        }
                    }
                    $roleEditAvatar = "EditAvatar";
                    if(isset($_POST['edit_avatar']) && $_POST['edit_avatar'] == $roleEditAvatar){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleEditAvatar){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleEditAvatar, "Avatar");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleEditAvatar){
                                $this->model_admin->deleteRole($gid, $roleEditAvatar);
                            }
                        }
                    }
                    $roleSizeAvatar = "SizeAvatar";
                    if(isset($_POST['size_avatar']) && $_POST['size_avatar'] == $roleSizeAvatar){
                        $ex = false;
                        foreach($role as $value){
                            if($value['RoleName'] == $roleSizeAvatar){
                                $ex = true;
                            }
                        }
                        if($ex == false){
                            $this->model_admin->addRole($gid, $roleSizeAvatar, "Avatar");
                        }
                    }else{
                        foreach($role as $value){
                            if($value['RoleName'] == $roleSizeAvatar){
                                $this->model_admin->deleteRole($gid, $roleSizeAvatar);
                            }
                        }
                    }
                }


                $role = $this->model_admin->getRoleByGroupId($gid);
                //Role
                $roleLoginAdmin = 0;
                $roleBanUser = 0;
                $roleGroupManagement = 0;
                $roleCreateThreads = 0;
                $roleViewThreads = 0;
                $roleEditThreats = 0;
                $roleDeleteThreads = 0;
                $roleCreateComment = 0;
                $roleCommentNotCheck = 0;
                $roleCommentColor = 0;
                $roleCreateSign = 0;
                $roleImageSign = 0;
                $roleSignSize = 0;
                $roleCreateAvatar = 0;
                $roleEditAvatar = 0;
                $roleSizeAvatar = 0;

                foreach($role as $value){
                    if($value['RoleName']=="LoginAdmin"){
                        $roleLoginAdmin = 1;
                    }
                    if($value['RoleName']=="BanUser"){
                        $roleBanUser = 1;
                    }
                    if($value['RoleName']=="GroupManagement"){
                        $roleGroupManagement = 1;
                    }
                    if($value['RoleName']=="CreateThreads"){
                        $roleCreateThreads = 1;
                    }
                    if($value['RoleName']=="ViewThreads"){
                        $roleViewThreads = 1;
                    } if($value['RoleName']=="EditThreats"){
                        $roleEditThreats = 1;
                    }
                    if($value['RoleName']=="DeleteThreads"){
                        $roleDeleteThreads = 1;
                    }
                    if($value['RoleName']=="CreateComment"){
                        $roleCreateComment = 1;
                    }
                    if($value['RoleName']=="CommentNotCheck"){
                        $roleCommentNotCheck = 1;
                    }
                    if($value['RoleName']=="CommentColor"){
                        $roleCommentColor = 1;
                    }
                    if($value['RoleName']=="CreateSign"){
                        $roleCreateSign = 1;
                    }
                    if($value['RoleName']=="ImageSign"){
                        $roleImageSign = 1;
                    }
                    if($value['RoleName']=="SignSize"){
                        $roleSignSize = 1;
                    }
                    if($value['RoleName']=="CreateAvatar"){
                        $roleCreateAvatar = 1;
                    }
                    if($value['RoleName']=="EditAvatar"){
                        $roleEditAvatar = 1;
                    }
                    if($value['RoleName']=="SizeAvatar"){
                        $roleSizeAvatar = 1;
                    }



                }

                $this->_show['roleLoginAdmin'] = $roleLoginAdmin;
                $this->_show['roleBanUser'] = $roleBanUser;
                $this->_show['roleGroupManagement'] = $roleGroupManagement;
                $this->_show['roleCreateThreads'] = $roleCreateThreads;
                $this->_show['roleViewThreads'] = $roleViewThreads;
                $this->_show['roleEditThreats'] = $roleEditThreats;
                $this->_show['roleDeleteThreads'] = $roleDeleteThreads;
                $this->_show['roleCreateComment'] = $roleCreateComment;
                $this->_show['roleCommentNotCheck'] = $roleCommentNotCheck;
                $this->_show['roleCommentColor'] = $roleCommentColor;
                $this->_show['roleCreateSign'] = $roleCreateSign;
                $this->_show['roleImageSign'] = $roleImageSign;
                $this->_show['roleSignSize'] = $roleSignSize;
                $this->_show['roleCreateAvatar'] = $roleCreateAvatar;
                $this->_show['roleEditAvatar'] = $roleEditAvatar;
                $this->_show['roleSizeAvatar'] = $roleSizeAvatar;

                $this->_show['name'] = $name;
                $this->_show['description'] = $description;
                $this->_show['date'] = $data;
                $this->_show['role'] = $role;
                $this->load->view('admin/layout1/body', $this->_show);

            }else{
                $url = base_url()."admin/group";
                header("Location:$url");
            }

        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }
    public function advert(){
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/advertising";
            $this->_show['date'] = "";
            $this->load->view('admin/layout1/body', $this->_show);
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }
    public function adv_new(){
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/adv_new";
            $this->_show['date'] = "";
            $this->load->view('admin/layout1/body', $this->_show);
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }
    public function category(){
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/category";
            $this->_show['date'] = "";
            $this->load->view('admin/layout1/body', $this->_show);
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }
    public function attachment(){
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/attachment";
            $data = $this->model_admin->getAllPlace();


            $this->_show['data'] = $data;
            $this->load->view('admin/layout1/body', $this->_show);
        }
    }
    public function config_website()
    {
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/config_website";
            $this->_show['date'] = "";
            $this->load->view('admin/layout1/body', $this->_show);
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }

    public function config_post()
    {
        $useInfor = $this->session->userdata('userInfo');
        if($useInfor){
            $this->_show['title'] = "Admin Management Page";
            $this->_show['view'] = "admin/config_post";
            $this->_show['date'] = "";
            $this->load->view('admin/layout1/body', $this->_show);
        }else{
            $url = base_url()."admin/login";
            header("Location:$url");
        }
    }
}

?>